//
//  HXVistorView.swift
//  DemoSinaWeibo
//
//  Created by Mon on 15/11/23.
//  Copyright © 2015年 Mon. All rights reserved.
//

import UIKit

protocol HXVistorViewDelegate: NSObjectProtocol
{
    func vistorViewLoginButtonClick()
    func vistorViewRigisterButtonClick()
}

class HXVistorView: UIView {
    
    //创建代理delegate属性
    weak var delegate: HXVistorViewDelegate?
    
    //一开始系统会调用init方法,故需要重写父类的init方法,使之来加载数据
    //在调用init方法的同时,需要设置控件的frame
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        //设置访客视图的背景颜色,使用调色板对比色差;alpha --> 表示透明度
        backgroundColor = UIColor.init(white: 237 / 255.0, alpha: 1)
        
        prepareUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //准备UI
    func prepareUI(){
        //1.添加子空间
        addSubview(iconView)
        addSubview(coverView)
        addSubview(homeView)
        addSubview(messageLabel)
        addSubview(loginButton)
        addSubview(registerButton)
        
        //2.添加约束,但是前提是先取消掉,系统自动添加约束机制
        //2.1关闭Autoresizing,不让它干扰autolayout的约束
        iconView.translatesAutoresizingMaskIntoConstraints = false
        coverView.translatesAutoresizingMaskIntoConstraints = false
        homeView.translatesAutoresizingMaskIntoConstraints = false
        messageLabel.translatesAutoresizingMaskIntoConstraints = false
        loginButton.translatesAutoresizingMaskIntoConstraints = false
        registerButton.translatesAutoresizingMaskIntoConstraints = false
        
        //3.为控件添加约束
        //3.1 --- iconView添加约束,与主视图垂直和水平中间对齐
            //垂直
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            //水平
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: -40))
        
        //3.2 --- homeView添加约束,与iconView垂直和水平中间对齐
            //垂直
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            //水平
        addConstraint(NSLayoutConstraint(item: homeView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        //3.3 --- messageView添加约束,与iconView底部有间距,与主控件中间对齐
            //x
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
            //y
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
//        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: messageLabel, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 226))
        
        //3.4 --- loginButton添加约束,与messageView左对齐,顶部有一个距离;固定width/height
            //top
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
            //left
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Right, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Right, multiplier: 1, constant: 0))
            //width
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
            //height
        addConstraint(NSLayoutConstraint(item: loginButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //3.5 --- registerButton添加约束,与messageView右对齐,顶部有一个距离;固定width/height
            //top
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 16))
            //left
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Left, relatedBy: NSLayoutRelation.Equal, toItem: messageLabel, attribute: NSLayoutAttribute.Left, multiplier: 1, constant: 0))
            //width
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 90))
            //height
        addConstraint(NSLayoutConstraint(item: registerButton, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1, constant: 35))
        
        //3.6 --- coverView添加约束,实现转盘下面的位置可以隐藏
            //width
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Width, multiplier: 1, constant: 0))
            //top
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
            //bottom
        addConstraint(NSLayoutConstraint(item: coverView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: loginButton, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
    }
    
    // MARK: - 按钮点击
    func loginClick(){
        delegate?.vistorViewLoginButtonClick()
    }
    func registerClick(){
        delegate?.vistorViewRigisterButtonClick()
    }
    
    
    // MARK: - 为iconView添加旋转事件
    func rotationAnimation(){
        //使用核心动画完成转盘的旋转
        let rotation = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画的参数
            //旋转周期为一周
        rotation.toValue = 2 * M_PI
            //完成一周旋转需要的时间(s)
        rotation.duration = 20
            //旋转重复的次数 --- 最大值
        rotation.repeatCount = MAXFLOAT
        //让iconView在视图切换的过程中也可以进行旋转
        rotation.removedOnCompletion = false
        //开始核心动画
        iconView.layer.addAnimation(rotation, forKey: "rotation")
    }
    
    /// 暂停旋转
    func pauseAnimation() {
        // 记录暂停时间
        let pauseTime = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil)
        
        // 设置动画速度为0
        iconView.layer.speed = 0
        
        // 设置动画偏移时间
        iconView.layer.timeOffset = pauseTime
    }
    
    /// 恢复旋转
    func resumeAnimation() {
        // 获取暂停时间
        let pauseTime = iconView.layer.timeOffset
        
        // 设置动画速度为1
        iconView.layer.speed = 1
        
        iconView.layer.timeOffset = 0
        
        iconView.layer.beginTime = 0
        
        let timeSincePause = iconView.layer.convertTime(CACurrentMediaTime(), fromLayer: nil) - pauseTime
        
        iconView.layer.beginTime = timeSincePause
    }
    
    // MARK: - 添加设置每个访客视图方法
    //创建该方法主要是实现每个访客视图都可以有自己的视图效果,分析发现:可变的就是iconView和messageLabel
    func setupInfo(imageName: String, message: String){
        
        //设置属性
        iconView.image = UIImage(named: imageName)
        messageLabel.text = message
        messageLabel.sizeToFit()
        //隐藏小房子
        homeView.hidden = true
 /// 方法一
//        coverView.hidden = true
 /// 方法二
//        bringSubviewToFront(iconView)
 /// 方法三
        sendSubviewToBack(coverView)
        
    }
    
    // MARK: - 懒加载
    //加载coverView -- 遮盖视图
    private lazy var coverView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
    //加载iconView --- 转盘视图
    private lazy var iconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    //加载homeView --- 房子
    private lazy var homeView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    //加载messageLabel --- 文本文字
    private lazy var messageLabel: UILabel = {
        let label = UILabel()
        
        //设置label属性
        label.text = "关注一些人,看看有没有什么惊喜"
        label.numberOfLines = 0
        label.textColor = UIColor.lightGrayColor()
        label.font = UIFont.systemFontOfSize(15)
        
        return label
    }()
    
    //加载loginButton --- 登陆按钮
    private lazy var loginButton: UIButton = {
        let button = UIButton()
        
        //设置button的属性
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.blackColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.sizeToFit()
        
        button.addTarget(self, action: "loginClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //加载registerButton --- 注册按钮
    private lazy var registerButton: UIButton = {
        let button = UIButton()
        
        //设置button属性
        button.setBackgroundImage(UIImage(named: "common_button_white_disable"), forState: UIControlState.Normal)
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setTitleColor(UIColor.orangeColor(), forState: UIControlState.Normal)
        button.titleLabel?.font = UIFont.systemFontOfSize(15)
        button.sizeToFit()
        
        button.addTarget(self, action: "registerClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
